/**
 * 获取评审人和抄送人数据
 * @param params 当前审批类别额外提供的参数
 */
function getReviewers(params) {
    // loading
    renderLoading('#reviewers-block', '');
    renderLoading('#cc-block', '');

    $.post('/approval/getReviewersByType', {
        type: getQueryString('type'),
        params: params
    }, function (response) {
        if (response.code) {
            alert(response.message);
        } else {
            reviewResult = response.data;
            renderUserList('reviewers', response.data.isRuleActive, response.data.reviewers, '#reviewers-block');
            renderUserList('cc', response.data.isRuleActive, response.data.cc, '#cc-block');

            // 特定条件下无需走审批通道
            if (response.data.isSystemAudit) {
                var dom = '<div class="alert alert-info">温馨提示：根据系统设定，当前审批单发起后将会自动通过，无需领导审批。</div>';
                $(dom).insertBefore($('.layui-form .layui-form-item:last-child button'));
            }
        }
    }, 'json');
}

/**
 * 初始化时间组件
 * @param el 目标元素
 */
function insDatetimePicker(el) {
    var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

    var options = {
        minView: 'month',
        language: 'zh-CN',
        format: 'yyyy-mm-dd',
        // weekStart: 1,
        todayBtn: true,
        autoclose: true,
        todayHighlight: true
    };
    if (!$.isEmptyObject(config)) {
        options = config;
    }
    // console.log(options);
    $(el).datetimepicker(options);
}

/**
 * 渲染loading组件
 * @param container
 * @param text
 */
function renderLoading(container, text = '加载中...') {
    $(container).html([
        '<i class="layui-icon icon-spin text-muted" style="font-size: 30px;">&#xe63d;</i>' + ' ' + text
    ].join(''));
}

/**
 * 计算时间差
 * @param sDate 开始时间
 * @param eDate 结束时间
 * @returns {object}
 */
function calcTimeDiff(sDate, eDate) {
    var days = 0, hours = 0, duration = '';
    var sDateTimestamp = formatDate(sDate);
    var eDateTimestamp = formatDate(eDate);
    var sDateArr = sDate.split(' ');
    var eDateArr = eDate.split(' ');
    var sDaysTimpstamp = formatDate(sDateArr[0]);
    var eDaysTimpstamp = formatDate(eDateArr[0]);

    // 拼接开始时间的下班时间并得到下班时间的时间戳
    var sOnWorkTimeString = sDateArr[0] + ' ' + global_configs.on_work_time;
    var sOffWorkTimeString = sDateArr[0] + ' ' + global_configs.off_work_time;
    var sOnWorkTimestamp = formatDate(sOnWorkTimeString);
    var sOffWorkTimestamp = formatDate(sOffWorkTimeString);

    // 拼接结束时间的下班时间并得到下班时间的时间戳
    var eOnWorkTimeString = eDateArr[0] + ' ' + global_configs.on_work_time;
    var eOffWorkTimeString = eDateArr[0] + ' ' + global_configs.off_work_time;
    var eOnWorkTimestamp = formatDate(eOnWorkTimeString);
    var eOffWorkTimestamp = formatDate(eOffWorkTimeString);

    if (eDaysTimpstamp == sDaysTimpstamp) {
        var sTime = sDateTimestamp < sOnWorkTimestamp ? sOnWorkTimestamp : sDateTimestamp;
        sTime = sTime > sOffWorkTimestamp ? sOffWorkTimestamp : sTime;

        var eTime = eDateTimestamp < eOnWorkTimestamp ? eOnWorkTimestamp : eDateTimestamp;
        eTime = eTime > eOffWorkTimestamp ? eOffWorkTimestamp : eTime;
        var obj = getDateTimeDifference(eTime, sTime);
        hours = obj.hours;
    } else if (eDaysTimpstamp > sDaysTimpstamp) {

        // 计算出开始时间的下班时间减去用户选择时间的时间戳
        var sDiff = sOffWorkTimestamp - sDateTimestamp;
        if (sDateTimestamp <= sOnWorkTimestamp) {
            days = 1;
        } else {
            if (sDiff > 0) {
                var sObj = getDateTimeDifference(sOffWorkTimestamp, sDateTimestamp);
                hours = hours + sObj.hours;
            }
        }
        // 计算出结束时间的下班时间减去用户选择时间的时间戳
        var eDiff = eDateTimestamp - eOnWorkTimestamp;
        if (eDateTimestamp >= eOffWorkTimestamp) {
            days++;
        } else {
            if (eDateTimestamp > eOnWorkTimestamp) {
                if (eDiff > 0) {
                    var eObj = getDateTimeDifference(eDateTimestamp, eOnWorkTimestamp);
                    hours = hours + eObj.hours;
                }
            }
        }

        var d = dateDiff(eDateArr[0], sDateArr[0]);
        // 如果天数相差大于1天才进行天数累加
        if (d > 1) {
            days = parseInt(days + d - 1);
        }
    } else {
        return '';
    }

    // 如果开始时间的上班加上结束时间的上班时间大于了上班时长也算1天
    if (hours > global_configs.work_time) {
        days++;
        hours = hours % global_configs.work_time;
    } else if (hours == global_configs.work_time) {
        days++;
        hours = 0;
    }
    if (days) {
        duration += days + '天';
    }
    if (hours) {
        duration += hours + '小时';
    }
    return {
        days: days,
        hours: hours,
        duration: duration
    };
}

/**
 * 获取地址栏参数
 * @param name 需要得到的参数名
 * @returns {string | null}
 */
function getQueryString(name)
{
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r!=null) return unescape(r[2]); return null;
}

/**
 * 获取两个日期相差的天数
 * @param end 结束时间
 * @param start 开始时间
 * @returns {number}
 */
function dateDiff(end, start) {
    var iDays = parseInt((formatDate(end) - formatDate(start)) / 1000 / 60 / 60 / 24); //把相差的毫秒数转换为天数
    return iDays;
}

/**
 * 获取两个日期相差的天数，小时数，和拼接的差值字符串
 * @param end
 * @param start
 * @returns {{duration: string, hours: number, days: number}}
 */
function datetimeDiff(end, start) {
    var duration = '';
    var obj = getDateTimeDifference(formatDate(end), formatDate(start));
    if (obj.days) {
        duration += obj.days + '天';
    }
    if (obj.hours) {
        duration += obj.hours + '小时';
    }
    return {
        days: obj.days,
        hours: obj.hours,
        duration: duration
    };
}

/**
 * 获取两个日期想差的天数，小时，分钟数，秒数
 * @param end 结束时间
 * @param start 开始时间
 * @returns {{hours: number, seconds: number, minutes: number, days: number}}
 */
function getDateTimeDifference(end, start) {
    var dateDiff = end - start;
    var days = Math.floor(dateDiff / (24 * 3600 * 1000));   // 计算出相差天数
    var leave1 = dateDiff % (24 * 3600 * 1000);   // 计算天数后剩余的毫秒数
    var hours = Math.ceil(leave1 / (3600 * 1000)); // 计算出小时数
    // 计算相差分钟数
    var leave2 = leave1 % (3600 * 1000);    // 计算小时数后剩余的毫秒数
    var minutes = Math.ceil(leave2 / (60 * 1000)); // 计算相差分钟数
    // 计算相差秒数
    var leave3 = leave2 % (60 * 1000); // 计算分钟数后剩余的毫秒数
    var seconds = Math.ceil(leave3 / 1000);
    return {
        days: days,
        hours: hours,
        minutes: minutes,
        seconds: seconds
    };
}

/**
 * 获取两个日期间相差的时长
 * @param end 结束时间
 * @param start 开始时间
 * @returns {{hours, seconds, minutes, days}}
 */
function timeDiff(end, start)
{
    return getTimeDifference(formatDate(end), formatDate(start));
}

/**
 * 获取两个日期间相差的时长
 * @param end 结束时间
 * @param start 开始时间
 * @returns {{hours, seconds, minutes, days}}
 */
function getTimeDifference(end, start) {
    var dateDiff = end - start;
    var days = Math.floor(dateDiff / (24 * 3600 * 1000));   // 计算出相差天数
    var hours = Math.ceil(dateDiff / (3600 * 1000)); // 计算出小时数
    // 计算相差分钟数
    var minutes = Math.ceil(dateDiff / (60 * 1000)); // 计算相差分钟数
    // 计算相差秒数
    var seconds = Math.ceil(dateDiff / 1000);
    return {
        days: days,
        hours: hours,
        minutes: minutes,
        seconds: seconds
    };
}

/**
 * 格式化日期
 * @param date 日期
 * @returns {number}
 */
function formatDate(date) {
    return new Date(date.replace(/-/g, "/")).getTime();
}
